﻿namespace move
{
    internal class Program
    {
        /// <summary>
        /// 实现光标移动
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //初始化坐标，给定范围
            int x = 0, y = 0, range = 100;
            String input = "begin";
            while (input != "Spacebar")
            {
                //清屏
                Console.Clear();
                //设置光标位置
                Console.SetCursorPosition(x, y);
                Console.Write("Hello!");
                //使光标位于"Hello!"首字母的位置，若无此行，光标将出现在"Hello!"后
                Console.SetCursorPosition(x, y);
                //Console.ReadKey()结果为ConsoleKeyInfo类，Key取得目前ConsoleKeyInfo物件表示的!主控台按鍵!，再通过ToString转为字符串
                //此处.Key再ToString之后空格的值就不是" "了，而是"Spacebar"
                input = Console.ReadKey(true).Key.ToString();
                switch (input)
                {
                    case "w":
                    case "W":
                        if (y > 0) y--;
                        break;
                    case "s":
                    case "S":
                        if (y < range) y++;
                        break;
                    case "a":
                    case "A":
                        if (x > 0) x--;
                        break;
                    case "d":
                    case "D":
                        if (x < range) x++;
                        break;
                    default:
                        if (input != "Spacebar")
                        {
                            Console.Write("输入错误！");
                            Console.ReadKey();
                        }
                            break;
                }

            }
            Console.Write("游戏结束！");
            Console.ReadKey();
        }
    }
}